package com.wn.androidflutterinnerwndemo

import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleObserver
import com.wn.androidflutterinnerwndemo.utils.LogUtils
import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.android.RenderMode
import io.flutter.embedding.android.TransparencyMode
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.plugin.common.MethodChannel
import java.lang.reflect.Method

/**
 * Author : wn
 * Email : maoning20080808@163.com
 * Date : 2025/7/26 10:12
 * Description : Android、Flutter相互通信
 */
class MutualCommunicationActivity : AppCompatActivity(), View.OnClickListener{

    var methodChannel : MethodChannel? = null;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.mutual_communication_main)
        addFlutterFragment()
        findViewById<Button>(R.id.mutual_comm_btn1).setOnClickListener(this)
        findViewById<Button>(R.id.mutual_comm_btn2).setOnClickListener(this)
    }

    private fun addFlutterFragment(){
        val flutterFragment = FlutterFragment.withCachedEngine("mutual_communication_engine_id")
            .renderMode(RenderMode.texture)
            .transparencyMode(TransparencyMode.transparent)
            .shouldAttachEngineToActivity(true)
            .build<FlutterFragment>()


        supportFragmentManager.beginTransaction()
            .add(R.id.mutual_comm_fragment_container, flutterFragment, "mutual_communication")
            .commit()

        //获取引擎
        val flutterEngine = FlutterEngineCache.getInstance().get("mutual_communication_engine_id")

        flutterEngine?.dartExecutor?.binaryMessenger?.let {
            methodChannel = MethodChannel(it, CHANNEL)
        }

        //提供给flutter调用
        methodChannel?.setMethodCallHandler{ call, result ->
            if("test1".equals(call.method)){
                LogUtils.d("flutter 调用 android")
            } else if ("test2".equals(call.method)){
                LogUtils.d("flutter 调用 android 有返回值")
                result.success("这是android 返回只 11")
            }
        }
    }

    override fun onClick(v: View?) {
        v?:return

        when(v.id){
            R.id.mutual_comm_btn1 -> {
                test3()
            }
            R.id.mutual_comm_btn2 -> {
                test4()
            }
        }
    }

    private fun test3(){
        methodChannel?.invokeMethod("test3" , null)
    }

    private fun test4(){
        methodChannel?.invokeMethod("test4", "android test4 参数", object : MethodChannel.Result{
            override fun success(result: Any?) {
                LogUtils.d("Android test4 success : ${result} ")
            }

            override fun error(errorCode: String, errorMessage: String?, errorDetails: Any?) {
                LogUtils.d("Android test4 success : ${errorCode} ")
            }

            override fun notImplemented() {
                LogUtils.d("Android test4 notImplemented ")
            }
        })
    }


    companion object {
        const val CHANNEL = "mutual_communication_channel"
    }

}